#include <stdio.h>
#include <stdlib.h>
#include <csp_spu.h>
#include <csp_debug.h>
#include "../../shared/pi.h"



int global_pid; 

double calc(double dx, double dy, double y, double y_max){

	double sum = 0.0;
	double x=0.0;
	while (y<y_max){
		x=0.0;
		while (x<1.0){
			sum=sum+1.0;
			x=x+dx;
			if ( (x*x+y*y)>=1.0 ){ 
				break; 
			}
		}
		y=y+dy;
	}
	return sum;
}


int Run(){
	int i __attribute__ ((aligned(16)));
	double output_sum __attribute__ ((aligned(16)));
	slave_arg_t sarg __attribute__ ((aligned(16)));

	csp_channel master2slaves;
	csp_channel slaves2master;

	master2slaves = csp_create_channel(ONE2ANY_CHANNEL, MASTER2SLAVES, sizeof(slave_arg_t));
	slaves2master = csp_create_channel(ANY2ONE_CHANNEL, SLAVES2MASTER, sizeof(double));


	printf("reading...\n");
	csp_channel_read(master2slaves, &sarg);
	
	printf("sarg.dx=%f, sarg.dy=%f, sarg.y=%f, sarg.y_max=%f\n", 
			sarg.dx, sarg.dy, sarg.y, sarg.y_max);
	
	output_sum = calc(sarg.dx, sarg.dy, sarg.y, sarg.y_max);

	printf("output_sum=%f\n", output_sum);

	csp_channel_write(slaves2master, &output_sum);



	return 0;
}
